﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Threading.Tasks;
using Dapper;
using JX.Core;
using JX.Core.Entity;
using JX.Infrastructure.Common;
using JX.Infrastructure.Data;
using JX.Infrastructure.Field;

namespace JX.Dapper
{
	/// <summary>
	/// 数据库表：Models 的仓储实现类.
	/// </summary>
	public partial class ModelsRepositoryDapper : RepositoryDapper<ModelsEntity>,IModelsRepositoryDapper
	{
		#region 得到节点绑定的模型列表：表：Model,NodesModelTemplate
		/// <summary>
		/// 得到节点绑定的模型列表
		/// </summary>
		/// <param name="nodeId"></param>
		/// <param name="enable">是否启用（true：启用；false：禁用；）</param>
		/// <param name="modelTyp">模型类型</param>
		/// <param name="writeAndRead"></param>
		/// <returns></returns>
		public DataTable GetModelListByNodeId(int nodeId, bool enable = true, ModelType modelTyp = ModelType.Content, WriteAndReadEnum writeAndRead = WriteAndReadEnum.None)
		{
			string strSql = "SELECT R.NodeID, R.ModelID, R.DefaultTemplateFile, M.ModelName, M.ItemName, M.ModelType, M.AddInfoFilePath, M.ManageInfoFilePath, M.PreviewInfoFilePath, M.BatchInfoFilePath, M.MaxPerUser FROM NodesModelTemplate R INNER JOIN Models M ON R.ModelId = M.ModelID WHERE R.NodeID = @NodeID AND M.IsDisabled = @IsDisabled ";
			var param = new DynamicParameters();
			param.Add("@NodeID", nodeId);
			param.Add("@IsDisabled", !enable);
			if (modelTyp != ModelType.None)
			{
				strSql = strSql + " AND M.ModelType = @ModelType ";
				param.Add("@ModelType",(int)modelTyp);
			}
			return GetDataTableBySql(strSql, param, CommandType.Text, writeAndRead);
		}
		#endregion

		#region 更新字段
		/// <summary>
		/// 更新字段
		/// </summary>
		/// <param name="modelType"></param>
		/// <param name="fieldList"></param>
		/// <returns></returns>
		public bool UpdateField(ModelType modelType, string fieldList)
		{
			return Update("Field = @Field", " and ModelType = @ModelType", new { Field = fieldList, ModelType = modelType });
		}
		/// <summary>
		/// 更新字段
		/// </summary>
		/// <param name="modelId"></param>
		/// <param name="fieldList"></param>
		/// <returns></returns>
		public bool UpdateField(int modelId, string fieldList)
		{
			return Update("Field = @Field", " and ModelID = @ModelID", new { Field = fieldList, ModelID = modelId });
		}
		#endregion

		#region 模型扩展表
		/// <summary>
		/// 添加模型字段列到模型扩展表
		/// </summary>
		/// <param name="fieldInfo"></param>
		/// <param name="tableName"></param>
		public void AddFieldToTable(FieldInfo fieldInfo, string tableName)
		{
			string strSql = "";
			if (fieldInfo.FieldType == FieldType.RegionType)
			{
				strSql = Query.GetAddRegionColumnToTableSql(fieldInfo, tableName);
			}
			else if (fieldInfo.FieldType == FieldType.ListBoxDataType && fieldInfo.Settings[3].ToLower() == "string")
			{
				strSql = Query.GetAddListBoxDataColumnToTableSql(fieldInfo, tableName);
			}
			else
			{
				strSql = Query.GetAddColumnToTableSql(fieldInfo, tableName);
			}
			ExeSQL(strSql);
		}
		/// <summary>
		/// 从指定的模型扩展表中，移除指定字段列
		/// </summary>
		/// <param name="fieldName"></param>
		/// <param name="tableName"></param>
		public void DeleteTableField(string fieldName, string tableName)
		{
			ExeSQL(Query.GetDeleteColumnFromTableSql(fieldName, tableName));
		}
		/// <summary>
		/// 从指定模型扩展表中，移除指定行政区域字段列
		/// </summary>
		/// <param name="fieldName"></param>
		/// <param name="tableName"></param>
		public void DeleteTableRegionField(string fieldName, string tableName)
		{
			ExeSQL(Query.GetDeleteRegionColumnFromTableSql(fieldName, tableName));
		}
		/// <summary>
		/// 从指定模型扩展表中，移除指定行政区域字段列
		/// </summary>
		/// <param name="fieldName"></param>
		/// <param name="tableName"></param>
		public void DeleteTableRegionFiveField(string fieldName, string tableName)
		{
			ExeSQL(Query.GetDeleteRegionFiveColumnFromTableSql(fieldName, tableName));
		}
		/// <summary>
		/// 更新模型扩展表中的字段列
		/// </summary>
		/// <param name="fieldInfo"></param>
		/// <param name="tableName"></param>
		public void UpdateTableField(FieldInfo fieldInfo, string tableName)
		{
			ExeSQL(Query.GetAlterColumnToTableSql(fieldInfo, tableName));
		}
		/// <summary>
		/// 删除模型扩展表
		/// </summary>
		/// <param name="modelId"></param>
		/// <returns></returns>
		public bool DeleteTable(int modelId)
		{
			string tableName = Get(modelId).TableName;
			string strSql = "DROP TABLE " + tableName;
			try
			{
				ExeSQL(strSql);
			}
			catch
			{
				return false;
			}
			return true;
		}

		/// <summary>
		/// 在指定表中查找指定字段的值列表，如果表名为“CommonInfo”，需要指定modelId
		/// </summary>
		/// <param name="tableName">模型表名</param>
		/// <param name="fieldName">模型字段名</param>
		/// <param name="modelId">模型ID</param>
		/// <param name="writeAndRead"></param>
		/// <returns></returns>
		public IList<string> GetLookupField(string tableName, string fieldName, int modelId,WriteAndReadEnum writeAndRead = WriteAndReadEnum.None)
		{
			string strSql = "SELECT " + fieldName + " FROM " + tableName;
			if (tableName == "CommonInfo")
			{
				strSql = strSql + " WHERE ModelID = " + modelId.ToString();
			}
			var list = GetListBySQL<string>(sql: strSql, writeAndRead: writeAndRead).AsList();
			if (list == null)
			{
				list = new List<string>();
			}
			return list;
		}
		#endregion
	}
}